<?php
namespace app\common\model\trends;

use think\Model;
use app\common\library\Auth;
use think\Db;
use traits\model\SoftDelete;

class Comment extends Model
{
	use SoftDelete;
	// 设置当前模型使用的完整数据库连接信息
	protected $connection = 'database.db_second';//连接配置

    // 表名
    protected $name = 'trends_comment';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
	    'is_like',
        'create_time_text',
	    'time_text',
	    'nickname',
	    'avatar'
    ];

    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return getDateText($data['create_time']);
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


	//获取用户信息
	public function getuser($row,$user_id,$circle_id){

//		$user = User::get($row->user_id);
//
		$data = [];
//		$data['nickname'] = $user->nickname;
//		$data['avatar'] = $user->avatar;
		//显示级别

		$circle = Db::connect($this->connection)->name('circle_user')->where(['user_id'=>$row->user_id,'circle_id'=>$row->circle_id])->find();

		if($circle){

			$data['level'] = $circle['level'];
			$level = Db::connect($this->connection)->name('publics_level')->where(['weigh'=>$circle['level']])->find();
			$nickname = Db::connect($this->connection)->name('circle_level')->where(['level'=>$level['weigh'],'circle_id'=>$row->circle_id])->value('nickname');
			$data['level_image'] = cdnurl($level['image'],true);
			$data['level_name'] = $nickname?$nickname:$level['name'];
			$data['icon'] = cdnurl($level['image'],true);
			$data['bg_color'] = $level['bg_color'];
			$data['font_color'] = $level['font_color'];
			if($row->user_id == $user_id && $circle['identity'] < 3){

				$data['identity'] = $circle['identity'];

			}else{
				$data['identity'] = 0;
			}

		}else{

			$circle = Db::connect($this->connection)->name('circle_user')->where(['user_id'=>$user_id,'circle_id'=>$circle_id])->find();
			$data['level'] = $circle?$circle['level']:1;
			$level = Db::connect($this->connection)->name('publics_level')->where(['weigh'=>$data['level']])->find();
			$nickname = Db::connect($this->connection)->name('circle_level')->where(['level'=>$level['weigh'],'circle_id'=>$row->circle_id])->value('nickname');
			$data['level_name'] = $nickname?$nickname:$level['name'];
			$data['icon'] = cdnurl($level['image'],true);
			$data['bg_color'] = $level['bg_color'];
			$data['font_color'] = $level['font_color'];
			$data['identity'] = $circle?$circle['identity']:0;

		}

		return $data;
	}



	/**
	 * 是否点赞
	 */
	public function getIsLikeAttr($value, $data){

		$user_id = Auth::instance()->id ?: -1;

		$res = Db::connect('database.db_second')->name("publics_likecoll")->where(['user_id'=>$user_id,'comment_id'=>$data['id']])->find();

		return $res ? 1 : 2;

	}

	public function getNicknameAttr($value, $data){

		$value = Db::name("user")->where(['id'=>$data['user_id']])->value('nickname');

		return $value ? $value : '';

	}


	public function getAvatarAttr($value, $data){

		$value = Db::name("user")->where(['id'=>$data['user_id']])->value('avatar');

		return $value ? $value : '';

	}


	public function getTimeTextAttr($value, $data)
	{
		$value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
		return is_numeric($value) ? date("Y-m-d H:i", $value) : $value;
	}


}
